﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ufida.u9.ait.openapi.utils;
using UFIDA.U9.Base;
using UFIDA.U9.Base.Currency;
using UFIDA.U9.CBO.Pub.Controller;
using UFIDA.U9.CBO.SCM.Item;
using UFIDA.U9.CBO.SCM.Warehouse;
using UFIDA.U9.InvDoc.TransferIn;
using UFIDA.U9.InvDoc.TransferOut;
using UFIDA.U9.ISV.TransferInISV;
using UFIDA.U9.ISV.TransferOutISV;
using UFSoft.UBF.Business;

namespace ufida.u9.ait.openapi
{
    internal class TransferOutBase
    {
        public static TransferOut ApproveDocEx(DocDTOData dto, TransferOut doc)
        {
            if (!String.IsNullOrEmpty(dto.DocDate))
            {
                //修改日期
                using (ISession session = Session.Open())
                {
                    doc.BusinessDate = DateTime.Parse(dto.DocDate);//单据日期
                    doc.SOBAccountPeriod = EntityUtils.getPostPeriod(doc.BusinessDate);//记账期间
                    if (doc.TransOutAccountPeriods != null && doc.TransOutAccountPeriods.Count > 0)
                    {
                        //记账期间
                        doc.TransOutAccountPeriods[0].SOBAccountPeriod = doc.SOBAccountPeriod;
                    }
                    foreach(var line in doc.TransOutLines)
                    {
                        foreach(var sline in line.TransOutSubLines)
                        {
                            sline.PlanRcvDate = doc.BusinessDate;//计划到货日期
                        }
                    }

                    session.Commit();
                }
            }
            return ApproveDoc(doc);
        }

        public static TransferOut ApproveDoc(TransferOut doc)
        {
            doc = doc.Key.GetEntity();
            if (doc.Status == Status.Approved)
            {
                throw new Exception($"单号[{doc.DocNo}]状态为已审,不能审核");
            }

            TransferOutBatchApproveSRV sv =new TransferOutBatchApproveSRV();
            sv.DocList = new List<CommonArchiveDataDTO> {
                new CommonArchiveDataDTO() {
                    ID=doc.ID,
                    Code=doc.DocNo,
                }
            };
            sv.Do();

            return doc;
        }

        public static TransferOut CreateDoc(TransferOutAITDTOData dto)
        {
            TransferOut doc = null;
            using (ISession session = Session.Open())
            {
                doc = TransferOut.Create();
                CreateHead(doc, dto);
                CreateLines(doc, dto);
                session.Commit();
            }

            return doc;
        }

        private static void CreateLines(TransferOut doc, TransferOutAITDTOData dto)
        {
            int rowno = 0;
            foreach (var linedto in dto.TransOutLines)
            {
                rowno += 10;
                TransOutLine line = TransOutLine.Create(doc);
                if (linedto.DocLineNo > 0)
                {
                    line.DocLineNo = linedto.DocLineNo;
                }
                else
                {
                    line.DocLineNo = rowno;
                }
                line.Org = doc.Org;
                line.TransOutOwnerOrg = doc.Org;
                line.TransOutOrg = doc.Org;
                //料品/单位
                if (linedto.ItemInfo == null) throw new Exception("ItemInfo必填");
                ItemMaster item = EntityUtils.GetEntityByCode<ItemMaster>(linedto.ItemInfo.ItemCode);
                line.ItemInfo = new ItemInfo()
                {
                    ItemID = item,
                    ItemCode = item.Code,
                    ItemName = item.Name,
                };
                line.StoreUOM = item.InventoryUOM;
                line.StoreBaseUOM = item.InventoryUOM.BaseUOM;
                line.CostUOM = item.CostUOM;
                line.CostBaseUOM = item.CostUOM.BaseUOM;

                //数量/单价/金额
                line.StoreUOMQty = linedto.StoreUOMQty;
                line.CostUOMQty = linedto.StoreUOMQty;
                //批号
                if (linedto.LotInfo != null)
                {
                    UFIDA.U9.Lot.LotMaster lotMaster = EntityUtils.GetLot(linedto.LotInfo.LotCode, item.Code);
                    line.LotInfo = new UFIDA.U9.Lot.LotMasterInfo()
                    {
                        LotMaster = lotMaster,
                        LotCode = lotMaster.LotCode,
                        LotValidDate = lotMaster.ValidDate,
                        DisabledDatetime = lotMaster.InvalidTime,
                    };
                }

                //其它
                line.SUTOSBURate = 1;
                line.SUToCURate = 1;
                line.CUTOCBURate = 1;
                line.Currency = EntityUtils.GetEntityByCode<Currency>("C001");
                line.CostCurrency = line.Currency;
                if (linedto.TransOutWh == null) throw new Exception($"TransOutWh必填");
                line.TransOutWh = EntityUtils.GetEntityByCode<Warehouse>(linedto.TransOutWh.Code);
                line.StorageType = UFIDA.U9.CBO.Enums.StorageTypeEnum.GetFromValue(4);
                
                //库位子表
                TransOutBin bin = TransOutBin.Create(line);
                bin.TimeForPickOrTally = doc.BusinessDate;
                bin.StoreUOMQty = line.StoreUOMQty;
                bin.BinLineNo = 10;
                bin.Org = doc.Org;

                CreateSubLine(line, linedto);
            }
        }

        private static void CreateSubLine(TransOutLine line, TransOutLineAITDTOData linedto)
        {
            TransOutSubLine subline = TransOutSubLine.Create(line);

            subline.TransOutSUQty = line.StoreUOMQty;
            subline.TransOutSU = line.StoreUOM;
            if (linedto.TransInWh == null) throw new Exception($"TransInWh 必填");
            subline.TransInWh = EntityUtils.GetEntityByCode<Warehouse>(linedto.TransInWh.Code);
            subline.TransInOwnerOrg = line.Org;
            subline.TransInOrg = line.Org;

            subline.SUTOSBURate = 1;
            subline.SUToCURate = 1;
            subline.SMUToSMBURate = 1;
            subline.TransOutToTransInRate = 1;
            subline.CUTOCBURate = 1;

            subline.StoreUOMQty = line.StoreUOMQty;
            subline.CostUOMQty = line.StoreUOMQty;

            subline.StoreUOM = line.StoreUOM;
            subline.StoreBaseUOM = line.StoreBaseUOM;
            subline.CostUOM = line.CostUOM;
            subline.CostBaseUOM = line.CostBaseUOM;

            subline.StorageType = line.StorageType;
            subline.PlanSendDate = line.TransferOut.BusinessDate;
            subline.PlanRcvDate = subline.PlanSendDate;
            subline.Org = line.Org;
            subline.ItemInfo = line.ItemInfo;
            subline.DocLineNo = 10;

            TransOutBin bin = TransOutBin.Create(subline);
            bin.TimeForPickOrTally = subline.PlanSendDate;
            bin.StoreUOMQty = line.StoreUOMQty;
            bin.BinLineNo = 10;

        }

        private static void CreateHead(TransferOut doc, TransferOutAITDTOData dto)
        {
            if (dto.TransOutDocType == null) throw new Exception("TransOutDocType必填");
            doc.TransOutDocType = EntityUtils.GetEntityByCode<TransOutDocType>(dto.TransOutDocType.Code);
            doc.DocNo = dto.DocNo;
            if (String.IsNullOrEmpty(dto.BusinessDate)) throw new Exception("BusinessDate必填");
            doc.BusinessDate = DateTime.Parse(dto.BusinessDate);
            doc.SOBAccountPeriod = EntityUtils.getPostPeriod(doc.BusinessDate);
            doc.PlanShippingDate = doc.BusinessDate;
            doc.LatestShippingDate = doc.BusinessDate;
            doc.LatestArriveDate = doc.BusinessDate;
            doc.ExpectArriveDate = doc.BusinessDate;
            doc.Org = Context.LoginOrg;

            //其它
            doc.IsVMIAAI = true;
        }
    }
}
